<?php
class Menu {
	var $tree=array();
	var $pid=0;
	var $title="未定义";
	var $url="#";
	var $leaf="true";
	var $comment="无";
	function Menu($jieku) {
		$this->jieku=$jieku;
	}
	function insert(){
		$db=$this->jieku->getDB();
		$db->exec(JDBTool::insert("sys_menus",array("pid"=>$this->pid,
									"title"=>$this->title,
									"url"=>$this->url,
									"leaf"=>$this->leaf,
									"comment"=>$this->comment),true));
									
	}
	function getMenuTree($pid){
		
//		$cache=$this->jieku->getCache();
//		$cache->setFileName("JieKu.Sys.Menu.(pid:$pid)");
//		$cachedate=$cache->get();
//		$menu=$this->getTree($pid);
//		if (!$cachedate){
//			$menu=$this->getTree($pid);
//			$cache->set($menu);
//		}
//		return $menu;
		return $this->getTree($pid);
	}
	function getTree($pid){
		$db=$this->jieku->getDB();
		$sql=JDBTool::select(array("id","pid","comment","title","title as text","url","leaf"),"sys_menus","pid='$pid' order by id ASC",true);
		$db->query($sql,PDO::FETCH_ASSOC);
		$menus=$db->getAll();
		if (count($menus)!=0){
			foreach ($menus as $key=>$menu){
				$menus[$key][children]=$this->getTree($menu[id]);
				if (count($menus[$key][children])==0){
					unset($menus[$key][children]);
				}else {
					$menus[$key][leaf]=false;
				}
			}
		}
		return $menus;
	}
}
?>